package com.example.dangerouscargodriver.ui.activity;

import androidx.annotation.Nullable;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baiju.bean.CityBean;
import com.baiju.bean.DistrictBean;
import com.baiju.bean.ProvinceBean;
import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.multipicker.HashMultiPickerView;
import com.baiju.style.multipicker.config.MultiPickerConfig;
import com.baiju.style.multipicker.listener.OnHashMultiPickerItemClickListener;
import com.baiju.style.multipicker.widget.bean.MultiItemBean;
import com.example.dangerouscargodriver.controller.AttrInfoController;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.controller.IControllerEventListener;
import com.example.dangerouscargodriver.entry.AddressInfo;
import com.example.dangerouscargodriver.entry.IdentifyInfo;
import com.example.dangerouscargodriver.entry.TruckClassInfo2;
import com.example.dangerouscargodriver.event.Event;
import com.example.dangerouscargodriver.event.LoadHttpResourceStatus;
import com.example.dangerouscargodriver.module.ResourceRecordInfo;
import com.example.dangerouscargodriver.entry.ResourceTypeInfo;
import com.example.dangerouscargodriver.entry.TruckClassInfo;
import com.example.dangerouscargodriver.entry.TruckTypeInfo;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.utils.AppManager;
import com.example.dangerouscargodriver.utils.DisplayUtil;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.baiju.style.citypickerview.CityPickerView;
import com.baiju.style.wheelpicker.WheelPickerView;
import com.baiju.style.wheelpicker.bean.WheelItemBean;
import com.baiju.style.wheelpicker.config.WheelPickerConfig;
import com.baiju.style.wheelpicker.listener.OnWheelItemClickListener;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import baiju.kit.datetimepicker.widget.DateTimePicker;

public class PublishResourceStepOneActivity extends HttpRequestActivity implements View.OnClickListener {

    private static final String TAG = "PublishResource";

    private static final int MSG_GET_IDENTIFY_INFO = 0;
    private static final int MSG_GET_ATTR_CORE = 1;

    private static final int START_ACTIVITY_FOR_START_DISTRICT  = 0;
    private static final int START_ACTIVITY_FOR_END_DISTRICT  = 1;

    private ActionBar mActionBar = null;
    private TextView mStartingStation = null;
    private TextView mEndingStation = null;
    private TextView mResourceType = null;
    private TextView mTextResourceCopy = null;
    private LinearLayout mLayoutResourceCopy = null;

    CityPickerView mCityPickerView = null;

    private ArrayList<WheelItemBean> mResTypeArray;
    private WheelPickerView mResTypePickerView = null;
    private boolean mLoadAttrCore = false;

    //货车类型选择
    private LinearLayout mLayoutCarType = null;
    private HashMap<String, ArrayList<MultiItemBean>> mHashMapCarTypeArray = null;
    private ArrayList<String> mHashKeyArray = null;
    private HashMultiPickerView mCarTypePickerView = null;
    //用户操作结果集
    private List<Integer> mSelectTruckCid = null;
    private String mSelectorTruck_cidKey = null;
    //智能识别结果集
    private List<TruckClassInfo2> mSelectTruckClass;

    private TextView mStartBaordingTime = null;
    private TextView mEndBaordingTime = null;
    private Date mStartDate, mEndDate;
    private DateTimePicker mStartDatePicker = null;
    private DateTimePicker mEndDatePicker = null;
    private TextView mNextStep = null;
    private TextView mTvCarTypeSelector = null;

    //货物重量
    private EditText mResourceWeight = null;
    //货物名称
    private EditText mResourceName = null;
    private EditText mStartDetailAddress = null;
    private EditText mEndDetailAddress = null;
    private EditText mAddressToIdentify = null;
    private TextView mIdentifyAction = null;

    private List<ResourceTypeInfo> mResourceTypeArray = null;
    private List<TruckClassInfo> mTruckClassArray = null;

    private HomeHandler mHandler = null;

    //货源类型选择存储
    private WheelItemBean mResourceTypeBean = null;
    //地址选择存储
    private ProvinceBean mSelectStartProvince = null;
    private CityBean mSelectStartCity = null;
    private DistrictBean mSelectStartDistrict = null;
    private ProvinceBean mSelectEndProvince = null;
    private CityBean mSelectEndCity = null;
    private DistrictBean mSelectEndDistrict = null;

    private MyControllerEventListener mControllerEventListener = null;

    private AddressInfo mSelectStartAddressInfo = null;
    private AddressInfo mSelectEndAddressInfo = null;
    private IdentifyInfo mIdentifyInfo = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(PublishResourceStepOneActivity.class);
        setContentView(R.layout.activity_publish_resource_step_one);
        AppManager.getAppManager().addActivity(this);

        mLayoutResourceCopy = findViewById(R.id.layout_resource_copy_write);
        mTextResourceCopy = findViewById(R.id.text_resource_copy_write);
        mTextResourceCopy.setOnClickListener(this);

        mSelectStartAddressInfo = new AddressInfo();
        mSelectEndAddressInfo = new AddressInfo();
        mSelectTruckCid = new ArrayList<>();

        mStartingStation = findViewById(R.id.ed_start_station);
        mStartingStation.setOnClickListener(this);
        mEndingStation = findViewById(R.id.ed_end_station);
        mEndingStation.setOnClickListener(this);
        mResourceType = findViewById(R.id.ed_resource_type);
        mResourceType.setOnClickListener(this);
        mSelectTruckClass = new ArrayList<>();

        mCityPickerView = new CityPickerView();
        mCityPickerView.init(this);

        mResTypeArray = new ArrayList<WheelItemBean>();
        mResTypePickerView = new WheelPickerView();
        mResTypePickerView.init(this, mResTypeArray);

        mLayoutCarType = findViewById(R.id.layout_car_type_selector);
        mLayoutCarType.setOnClickListener(this);
        mTvCarTypeSelector = findViewById(R.id.tv_car_type_selector);
        mCarTypePickerView = new HashMultiPickerView();
        mHashMapCarTypeArray = new HashMap<>();
        mHashKeyArray = new ArrayList<>();
        mCarTypePickerView.init(this, mHashMapCarTypeArray, mHashKeyArray);

        mStartBaordingTime = findViewById(R.id.ed_start_boarding_time);
        mStartBaordingTime.setOnClickListener(this);

        mEndBaordingTime = findViewById(R.id.ed_end_boarding_time);
        mEndBaordingTime.setOnClickListener(this);

        mResourceWeight = findViewById(R.id.ed_resource_weight);

        mStartDetailAddress = findViewById(R.id.ed_start_detail_address);
        mEndDetailAddress = findViewById(R.id.ed_end_detail_address);

        mAddressToIdentify = findViewById(R.id.ed_address_to_identify);
        mIdentifyAction = findViewById(R.id.btn_to_identify);
        mIdentifyAction.setOnClickListener(this);

        mResourceName = findViewById(R.id.tv_resource_name);
        mResourceName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    // 隐藏输入法
                    InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    // 显示或者隐藏输入法
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        });

        mNextStep = findViewById(R.id.btn_next_step);
        mNextStep.setOnClickListener(this);

        mActionBar = findViewById(R.id.actionBarPublicResourceStepOne);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        init();
    }

    private void init(){
        mResourceTypeArray = new ArrayList<>();
        mTruckClassArray = new ArrayList<>();
        mHandler = new HomeHandler(this);

        mControllerEventListener = new MyControllerEventListener();
        AttrInfoController.Instance().attachControllerListener(PublishResourceStepOneActivity.class,
                mControllerEventListener);
    }

    private void editTextComponentLoseFocus(){
        mStartDetailAddress.clearFocus();
        mEndDetailAddress.clearFocus();
        mResourceName.clearFocus();
        mResourceWeight.clearFocus();
    }

    private boolean checkUserInputSituation(){
        if(mResourceTypeBean == null ||
                mResourceWeight.getText() == null ||
                mResourceWeight.getText().toString().equals("") ||
                mSelectTruckCid == null ||
                mSelectTruckCid.size() == 0 ||
                mStartBaordingTime.getText() == null ||
                mStartBaordingTime.getText().toString().equals("") ||
                mEndBaordingTime.getText() == null ||
                mEndBaordingTime.getText().toString().equals("") ||
                mResourceName.getText() == null ||
                mResourceName.getText().toString().equals("") ||
                mSelectStartAddressInfo == null ||
                mSelectEndAddressInfo == null)
        {
            return false;
        }
        else{
            return true;
        }
    }

    private void identifyAddress(){
        if(mAddressToIdentify.getText().toString() == null ||
            mAddressToIdentify.getText().toString().equals("")){
            ToastUtils.showLongToast(this, "请粘贴有效的地址信息！");
        }
        else {
            HashMap<String, String> header = new HashMap<String, String>();
            header.put("token", AuthController.Instance().getToken());

            HashMap<String, String> resource_param = new HashMap<String, String>();
            resource_param.put("sgData", mAddressToIdentify.getText().toString());

            sendHttpRequest(
                    IHttpRequest.RequestMethod.POST_METHOD,
                    RemoteAPI.REQUEST_V1_SG_DATA_DISCERN,
                    header,
                    resource_param,
                    RemoteAPICmd.REQUEST_V1_SG_DATA_DISCERN);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AttrInfoController.Instance().unAttachControllerListener(
                PublishResourceStepOneActivity.class,
                mControllerEventListener);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.ed_start_station){
            Intent intent = new Intent(this, ProvinceCityDistrictPickerActivity.class);
            intent.putExtra("type", ProvinceCityDistrictPickerActivity.AREA_SELECT_DISTRICT);
            intent.putExtra("province", mSelectStartProvince);
            intent.putExtra("city", mSelectStartCity);
            intent.putExtra("district", mSelectStartDistrict);
            startActivityForResult(intent, START_ACTIVITY_FOR_START_DISTRICT);
        }
        else if(v.getId() == R.id.ed_end_station){
            Intent intent = new Intent(this, ProvinceCityDistrictPickerActivity.class);
            intent.putExtra("type", ProvinceCityDistrictPickerActivity.AREA_SELECT_DISTRICT);
            intent.putExtra("province", mSelectEndProvince);
            intent.putExtra("city", mSelectEndCity);
            intent.putExtra("district", mSelectEndDistrict);
            startActivityForResult(intent, START_ACTIVITY_FOR_END_DISTRICT);
        }
        else if(v.getId() == R.id.ed_resource_type){
            if(!mLoadAttrCore){
                return;
            }
            editTextComponentLoseFocus();
            WheelPickerConfig pickerConfig = new WheelPickerConfig.Builder()
                    .title("选择类型")
                    .visibleItemsCount(8)
                    .defaultItem(mResTypeArray.get(3).getName())
                    .itemCyclic(false)
                    .setCustomItemLayout(R.layout.item_city)//自定义item的布局
                    .setCustomItemTextViewId(R.id.item_city_name_tv)
                    .build();
            pickerConfig.setTitleBackgroundColorStr("#FFD401");

            mResTypePickerView.setConfig(pickerConfig);
            mResTypePickerView.setOnItemClickListener(new OnWheelItemClickListener() {
                @Override
                public void onSelected(WheelItemBean type) {
                    if(type != null){
                        mResourceTypeBean = type;
                        addResourceType(type);
                    }
                }

                @Override
                public void onCancel() {
                    /**/
                }
            });
            mResTypePickerView.showWheelPicker();
        }
        else if(v.getId() == R.id.layout_car_type_selector){
            if(!mLoadAttrCore){
                return;
            }
            editTextComponentLoseFocus();
            MultiPickerConfig pickerConfig = new MultiPickerConfig.Builder()
                    .title("选择类型")
                    .setCustomItemLayout(R.layout.item_city)//自定义item的布局
                    .setCustomItemTextViewId(R.id.item_city_name_tv)
                    .build();
            pickerConfig.setTitleBackgroundColorStr("#FFD401");

            mCarTypePickerView.setConfig(pickerConfig);
            mCarTypePickerView.setOnMultiItemClickListener(new OnHashMultiPickerItemClickListener() {
                public void onSelected(String selectKey, List<Integer> select) {
                    Log.d(TAG, "selectKey: " + selectKey);
                    Log.d(TAG, "select: " + select.toString());
                    mSelectTruckCid = select;
                    mSelectorTruck_cidKey = selectKey;
                    addCarTypeItem(selectKey, select);
                }

                @Override
                public void onCancel() {
                    /**/
                }
            });

            mCarTypePickerView.showHashMultiPicker();
        }
        else if(v.getId() == R.id.ed_start_boarding_time){
            editTextComponentLoseFocus();
            Calendar calendar = Calendar.getInstance();
            Date startDate = calendar.getTime();

            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 8);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            Date endDate = calendar.getTime();

            DateTimePicker.Builder builder = new DateTimePicker.Builder(this)
                    .setTitle("选择年月日")
                    .setCancelTextColor(Color.GRAY)
                    .setOkTextColor(Color.RED)
                    .setTitleTextColor(0xFF999999)
                    .setSelectedTextColor(Color.RED)
                    .setKeepLastSelected(true)
                    .setShowYMDHMLabel(true)
                    .setShowType(DateTimePicker.ShowType.DAY);
            mStartDatePicker = new DateTimePicker(this, new DateTimePicker.ResultHandler() {
                @Override
                public void handle(Date date) {
                    PublishResourceStepOneActivity.this.mStartDate = date;
                    String currentDateTimeString =
                            new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(date);
                    mStartBaordingTime.setText(currentDateTimeString);
                }
            }, startDate, endDate, builder);
            mStartDatePicker.show(startDate);
        }
        else if(v.getId() == R.id.ed_end_boarding_time){
            editTextComponentLoseFocus();
            Calendar calendar = Calendar.getInstance();
            Date startDate = calendar.getTime();

            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 8);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            Date endDate = calendar.getTime();

            DateTimePicker.Builder builder = new DateTimePicker.Builder(this)
                    .setTitle("选择年月日")
                    .setCancelTextColor(Color.GRAY)
                    .setOkTextColor(Color.RED)
                    .setTitleTextColor(0xFF999999)
                    .setSelectedTextColor(Color.RED)
                    .setKeepLastSelected(true)
                    .setShowYMDHMLabel(true)
                    .setShowType(DateTimePicker.ShowType.DAY);
            mEndDatePicker = new DateTimePicker(this, new DateTimePicker.ResultHandler() {
                @Override
                public void handle(Date date) {
                    PublishResourceStepOneActivity.this.mEndDate = date;
                    String currentDateTimeString =
                            new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(date);
                    mEndBaordingTime.setText(currentDateTimeString);
                }
            }, startDate, endDate, builder);
            mEndDatePicker.show(startDate);
        }
        else if(v.getId() == R.id.btn_to_identify){
            identifyAddress();
        }
        else if(v.getId() == R.id.btn_next_step){
            //!TODO 填写有效性的检查
            if(!checkUserInputSituation()){
                ToastUtils.showLongToast(getBaseContext(), "请填写完整的信息！");
            }
            else {
                ResourceRecordInfo recordInfo = new ResourceRecordInfo();
                recordInfo.setClass_id(mResourceTypeBean.getId());
                recordInfo.setWeight(mResourceWeight.getText().toString());
                String truck_cid = "";
                String truck_cname = "";
                for (int index = 0; index < mSelectTruckCid.size(); index++) {
                    truck_cid += mHashMapCarTypeArray.get(mSelectorTruck_cidKey).get(mSelectTruckCid.get(index)).getId() + ",";
                    truck_cname += mHashMapCarTypeArray.get(mSelectorTruck_cidKey).get(mSelectTruckCid.get(index)).getName() + "/";
                }
                if (truck_cid.length() > 0) {
                    truck_cid = truck_cid.substring(0, truck_cid.length() - 1);
                    recordInfo.setTruck_cid(truck_cid);
                }
                if (truck_cname.length() > 0) {
                    truck_cname = truck_cname.substring(0, truck_cname.length() - 1);
                    recordInfo.setTruck_cname(truck_cname);
                }
                recordInfo.setTruck_type(mSelectorTruck_cidKey);

                recordInfo.setLoadtime(mStartBaordingTime.getText().toString());
                recordInfo.setLoadtime2(mEndBaordingTime.getText().toString());
                recordInfo.setName(mResourceName.getText().toString());
                recordInfo.setFrom_address(mSelectStartAddressInfo.getName());
                recordInfo.setFrom_address_id(mSelectStartAddressInfo.getId());
                recordInfo.setTo_address(mSelectEndAddressInfo.getName());
                recordInfo.setTo_address_id(mSelectEndAddressInfo.getId());
                if(mIdentifyInfo != null) {
                    recordInfo.setContact(mIdentifyInfo.getName());
                    recordInfo.setMobliephone(mIdentifyInfo.getPhone());
                }
                
                Intent intent = new Intent(this, PublishResourceStepTwoActivity.class);
                intent.putExtra("info", recordInfo);
                startActivity(intent);
            }
        }
        else if(v.getId() == R.id.text_resource_copy_write){
            mTextResourceCopy.setVisibility(View.GONE);
            mLayoutResourceCopy.setVisibility(View.VISIBLE);
        }
        else{

        }
    }

    private void addResourceType(WheelItemBean type) {
        mResourceType.setText(type.getName());
    }

    private void addCarTypeItem(String selectKey, List<Integer> select) {
        if(select == null){
            return;
        }

        mLayoutCarType.removeAllViews();
        LayoutInflater layoutInflater = LayoutInflater.from(getBaseContext());
        for(int index = 0; index < select.size(); index++){
            View itemLayout = layoutInflater.inflate(R.layout.item_search_bar, null);

            ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(DisplayUtil.dp2px(getBaseContext(), 0),
                    0,
                    12,
                    0);
            itemLayout.setLayoutParams(params);

            TextView text = itemLayout.findViewById(R.id.item_show_str);
            text.setText(mHashMapCarTypeArray.get(selectKey).get(select.get(index)).getName());
            itemLayout.setBackgroundResource(R.drawable.rounded_button);
            mLayoutCarType.addView(itemLayout);
        }
    }

    private void addCarTypeItem(List<TruckClassInfo2> select) {
        if(select == null){
            return;
        }

        mSelectTruckClass = select;
        mLayoutCarType.removeAllViews();
        LayoutInflater layoutInflater = LayoutInflater.from(getBaseContext());
        for(int index = 0; index < select.size(); index++){
            View itemLayout = layoutInflater.inflate(R.layout.item_search_bar, null);

            ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(DisplayUtil.dp2px(getBaseContext(), 0),
                    0,
                    12,
                    0);
            itemLayout.setLayoutParams(params);

            TextView text = itemLayout.findViewById(R.id.item_show_str);
            text.setText(select.get(index).getName());
            itemLayout.setBackgroundResource(R.drawable.rounded_button);
            mLayoutCarType.addView(itemLayout);
        }
    }

    private void initResourceTypeInfo(){
        mResTypeArray.clear();
        if(AttrInfoController.Instance().getResourceTypeArray() != null &&
                AttrInfoController.Instance().getResourceTypeArray().size() > 0){
            for(int index = 0; index < AttrInfoController.Instance().getResourceTypeArray().size(); index++){
                WheelItemBean bean = new WheelItemBean();
                bean.setName(AttrInfoController.Instance().getResourceTypeArray().get(index).getName());
                bean.setId(AttrInfoController.Instance().getResourceTypeArray().get(index).getId());
                mResTypeArray.add(bean);
            }
        }
        Log.d(TAG, "mResTypeArray: " + mResTypeArray.toString());
    }

    private void initTruckClassInfo(){
        mHashMapCarTypeArray.clear();
        mHashKeyArray.clear();
        if(AttrInfoController.Instance().getTruckTypeArray() != null &&
                AttrInfoController.Instance().getTruckTypeArray().size() > 0){
            for(int index = 0; index < AttrInfoController.Instance().getTruckTypeArray().size(); index++){
                TruckTypeInfo truckTypeInfo = AttrInfoController.Instance().getTruckTypeArray().get(index);
                List<TruckClassInfo> list = truckTypeInfo.getList();
                ArrayList<MultiItemBean> arrayList = new ArrayList<>();
                for(int index2 = 0; index2 < list.size(); index2++) {
                    MultiItemBean bean = new MultiItemBean();
                    bean.setName(list.get(index2).getClassname());
                    bean.setId(list.get(index2).getClassid());
                    bean.setSelected("0");
                    arrayList.add(bean);
                }
                mHashMapCarTypeArray.put(truckTypeInfo.getType(), arrayList);
                mHashKeyArray.add(truckTypeInfo.getType());
            }
        }
    }

    private void fillIdentifyInfo(IdentifyInfo info){
        //填补填写完整性判断对应的资源信息
        mSelectStartAddressInfo = info.getInfo().get(0).getArea_data().get(0);
        mSelectEndAddressInfo = info.getInfo().get(0).getArea_data().get(1);

        //！TODO:如果用户输入的信息不标准，既包含罐车又包含非罐车的信息要如何处理???
        mSelectTruckCid.clear();
        for (String key : mHashMapCarTypeArray.keySet()) {
            ArrayList<MultiItemBean> arrayList = mHashMapCarTypeArray.get(key);
            for(int index = 0; index < arrayList.size(); index++){
                arrayList.get(index).setSelected("0");
            }
            boolean find = false;
            for(int index = 0; index < arrayList.size(); index++){
                MultiItemBean bean = arrayList.get(index);
                for(int index2 = 0; index2 < info.getInfo().get(0).getTruck_class().size(); index2++) {
                    TruckClassInfo2 truckClassInfo2 = info.getInfo().get(0).getTruck_class().get(index2);
                    if (bean.getId().equals(truckClassInfo2.getId())){
                        find = true;
                        bean.setSelected("1");
                        mSelectTruckCid.add(index);
                    }
                }
                if(find){
                    mSelectorTruck_cidKey = key;
                }
            }
        }

        if(mResourceTypeBean == null){
            mResourceTypeBean = new WheelItemBean();
        }
        mResourceTypeBean.setId(info.getInfo().get(0).getCore_attr().getId());
        mResourceTypeBean.setName(info.getInfo().get(0).getCore_attr().getName());

        //填充页面资源
        mStartingStation.setText(mSelectStartAddressInfo.getName());
        mEndingStation.setText(mSelectEndAddressInfo.getName());
        mResourceWeight.setText(info.getInfo().get(0).getHeft());
        mResourceName.setText(info.getInfo().get(0).getSg_name());
        addCarTypeItem(info.getInfo().get(0).getTruck_class());
        if(info.getInfo().get(0).getCore_attr() != null) {
            WheelItemBean bean = new WheelItemBean();
            bean.setId(info.getInfo().get(0).getCore_attr().getId());
            bean.setName(info.getInfo().get(0).getCore_attr().getName());
            addResourceType(bean);
        }

        Date loadtime = new Date();
        loadtime.setTime(Long.parseLong(info.getTime()));
        String currentDateTimeString =
                new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(loadtime);
        mStartBaordingTime.setText(currentDateTimeString);
    }

    private static class HomeHandler extends Handler {
        private String TAG = "HomeHandler";
        private final WeakReference<PublishResourceStepOneActivity> mActivity;

        HomeHandler(PublishResourceStepOneActivity activity) {
            mActivity = new WeakReference<PublishResourceStepOneActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            PublishResourceStepOneActivity activity = mActivity.get();
            switch (msg.what){
                case MSG_GET_ATTR_CORE:
                    activity.mLoadAttrCore = true;
                    activity.initResourceTypeInfo();
                    activity.initTruckClassInfo();
                    break;
                default:
                    break;
            }
        }
    }


    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V1_SG_DATA_DISCERN){
                Log.d(TAG, "onResponse: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    IdentifyInfo identifyInfo = gson.fromJson(res.get("data").toString(),
                            IdentifyInfo.class);

                    if(identifyInfo != null) {
                        mIdentifyInfo = identifyInfo;
                        fillIdentifyInfo(identifyInfo);
                    }
                }
                else{
                    /**/
                }
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == START_ACTIVITY_FOR_START_DISTRICT){
            if(data != null) {
                mSelectStartDistrict = (DistrictBean) data.getParcelableExtra("district");
                mSelectStartCity = (CityBean) data.getParcelableExtra("city");
                mSelectStartProvince = (ProvinceBean) data.getParcelableExtra("province");
                String select = mSelectStartProvince.getName() + " " +
                        mSelectStartCity.getName() + " " +
                        mSelectStartDistrict.getName();
                mSelectStartAddressInfo.setName(select);
                mSelectStartAddressInfo.setId("1-" + mSelectStartProvince.getId() + "-" +
                        mSelectStartCity.getId() + "-" +
                        mSelectStartDistrict.getId());
                mStartingStation.setText(mSelectStartAddressInfo.getName());
            }
        }
        else if(requestCode == START_ACTIVITY_FOR_END_DISTRICT){
            if(data != null) {
                mSelectEndDistrict = (DistrictBean) data.getParcelableExtra("district");
                mSelectEndCity = (CityBean) data.getParcelableExtra("city");
                mSelectEndProvince = (ProvinceBean) data.getParcelableExtra("province");
                String select = mSelectEndProvince.getName() + " " +
                        mSelectEndCity.getName() + " " +
                        mSelectEndDistrict.getName();
                mSelectEndAddressInfo.setName(select);
                mSelectEndAddressInfo.setId("1-" + mSelectEndProvince.getId() + "-" +
                        mSelectEndCity.getId() + "-" +
                        mSelectEndDistrict.getId());
                mEndingStation.setText(mSelectEndAddressInfo.getName());
            }
        }
        else{
            /**/
        }
    }

    private class MyControllerEventListener implements IControllerEventListener {

        @Override
        public void onEvent(int actionId, String json) {
            if(actionId == Event.EVENT_LOAD_HTTP_RESOURCE_CORE_ATTR){
                Gson gson = new Gson();
                LoadHttpResourceStatus status = gson.fromJson(json, LoadHttpResourceStatus.class);
                Log.d(TAG, "onEvent: " + status.toJson());
                if(status.getWhat() == AttrInfoController.EVENT_ATTR_LOAD_DONE){
                    Message msg = new Message();
                    msg.what = MSG_GET_ATTR_CORE;
                    mHandler.sendMessage(msg);
                }
            }
            else{
                /**/
            }
        }
    }
}